package cn.xutongxue.book_sales.controller;


import cn.xutongxue.book_sales.base.Result;
import cn.xutongxue.book_sales.entity.Address;
import cn.xutongxue.book_sales.service.AddressService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author XuTongXue
 * @since 2021-12-02
 */
@RestController
@RequestMapping("/address")
public class AddressController {

    @Resource
    private AddressService addressService;

    @GetMapping("/get")
    public Result get(String address_user_id) {

        System.out.println(address_user_id);
        QueryWrapper<Address> wrapper = new QueryWrapper<>();
        wrapper.eq("address_user_id", address_user_id);
        Address address = addressService.getOne(wrapper);
        System.out.println(address);
        return Result.ok(address);

    }
    @PostMapping("/add")
    public Result add(@RequestBody Address address) {

        System.out.println(address);
        address.setAddressUserSex(Integer.valueOf(address.getAddressUserSex()));

        addressService.save(address);
        if(address.getAddressId() != null) {
            return Result.ok("添加成功",null);
        }
        return Result.fail(202,"添加失败");
    }

    @PostMapping("/update")
    public Result update(@RequestBody Address address) {

        if (addressService.update(address,new QueryWrapper<Address>().eq("address_user_id",address.getAddressUserId()))) {
            return Result.ok("修改成功",null);
        }
        return Result.fail(203,"修改失败");

    }

    @PostMapping("/delete")
    public Result delete(Long address_id) {
        if (addressService.removeById(address_id)) {
            return Result.ok("删除成功");
        }
        return Result.fail(204,"删除失败");
    }
}
